from django.conf import settings
from django_redis import get_redis_connection
from rest_framework import serializers
from itsdangerous import TimedJSONWebSignatureSerializer as TJS
from rest_framework_jwt.settings import api_settings

from oauth.models import OAuthQQUser
from users.models import User

"""
mobile	        str	是	手机号
password	    str	是	密码
sms_code	    str	是	短信验证码
access_token	str	是	凭据 （包含openid)
"""


class OAuthQQSerializer(serializers.Serializer):
    """用户绑定的序列化器"""
    mobile = serializers.RegexField(regex='1[3-9]\d{9}')
    password = serializers.CharField(max_length=20, min_length=8, write_only=True)
    sms_code = serializers.CharField(max_length=6, min_length=6, write_only=True)
    access_token = serializers.CharField(write_only=True)
    token = serializers.CharField(read_only=True)
    username = serializers.CharField(read_only=True)

    def validate(self, attrs):

        # 判断access_token
        # 对access_token进行解密
        tjs = TJS(settings.SECRET_KEY, 300)
        try:
            data = tjs.loads(attrs['access_token'])
        except:
            raise serializers.ValidationError('错误的access_token')
        # 获取openid数据
        openid = data.get('openid')
        if not openid:
            raise serializers.ValidationError('access_token失效')

        # 添加attrs属性
        attrs['openid'] = openid

        # 判断短信
        # 先获取缓存中短信
        conn = get_redis_connection('verify')
        real_sms_code = conn.get('sms_code_%s' % attrs['mobile']).decode()

        if not real_sms_code:
            raise serializers.ValidationError('验证码已过期')

        if attrs['sms_code'] != real_sms_code:
            raise serializers.ValidationError('验证码输入不正确')

        # 验证用户
        try:
            user = User.objects.get(mobile=attrs['mobile'])

            if user.check_password(attrs['password']):
                attrs['user'] = user
                return attrs

            raise serializers.ValidationError('密码不正确')
            # return None
        except User.DoesNotExist:
            return attrs

    def create(self, validated_data):
        # 获取用户
        user = validated_data.get('user', None)
        self.context['view'].user = user

        if user is None:
            # 用户不存在 创建新用户
            print(validated_data['mobile'])
            # user = User.objects.create_user(username=validated_data['mobile'],
            # mobile=validated_data['mobile'],password=validated_data['password'])
            user = User.objects.create_user(username=validated_data['mobile'], mobile=validated_data['mobile'],
                                            password=validated_data['password'])

        # 绑定
        OAuthQQUser.objects.create(user=user, openid=validated_data['openid'])

        # 生成token
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

        user.token = token
        return user

# class OAuthQQSerializer(serializers.Serializer):
#
#     mobile = serializers.RegexField(regex=(r'1[3-9]\d{9}'))
#     sms_code = serializers.CharField(min_length=6,max_length=6,write_only=True)
#     password = serializers.CharField(min_length=8,max_length=20,write_only=True)
#     access_token = serializers.CharField(write_only=True)
#     username = serializers.CharField(min_length=8,max_length=20,read_only=True)
#
#     def validate(self, attrs):
#
#         # 查询openid是否正确
#         tjs = TJS(settings.SECRET_KEY,300)
#         try:
#             data = tjs.loads(attrs['access_token'])
#         except:
#             raise serializers.ValidationError('access_token已经过期')
#
#         attrs['openid'] = data.get('openid')
#
#         # 查询短信验证码是否正确
#         conn = get_redis_connection('verify')
#         real_sms_code = conn.get('sms_code_%s' %attrs['mobile']).decode()
#         # print(real_sms_code)
#
#         if not real_sms_code:
#             raise serializers.ValidationError('验证码已经过期')
#
#         if real_sms_code != attrs['sms_code']:
#             raise serializers.ValidationError('验证码填写错误')
#
#         # 验证码密码是否正确
#         # 通过手机号去查询用户是否已经登录过
#         try:
#             user = User.objects.get(mobile=attrs['mobile'])
#
#             if user.check_password(attrs['password']):
#                 raise serializers.ValidationError('密码填写错误')
#             attrs['user'] = user
#             return attrs
#         except:
#             return attrs
#
#     def create(self, validated_data):
#         user = validated_data.get('user',None)
#
#         if user is None:
#             user = User.objects.create_user(username=validated_data['mobile'],mobile=validated_data['mobile'],password=validated_data['password'])
#
#         # 绑定到qq绑定用户数据库中
#         OAuthQQUser.objects.create(user=user,openid=validated_data['openid'])
#
#         # 生成token
#
#         jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
#         jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
#
#         payload = jwt_payload_handler(user)
#         token = jwt_encode_handler(payload)
#
#         user.token = token
#
#         return user
